<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Base_object</title>
</head>
<body>

</body>
<script>
    let b = Number('123');
    console.log(typeof ( b));

    let a = Boolean('true');
    console.log(typeof (a));

    let now = new Date();
    console.log(now);
    console.log(typeof (now));
    console.log(now.getFullYear());
    console.log(now.getMonth());
    console.log(now.getDate());
    console.log(now.getDay());
    console.log(now.getHours());
    console.log(now.getMinutes());
    console.log(now.getSeconds());
    console.log(now.getMilliseconds());
    console.log(now.getTime());
    console.log(now.toDateString());
    console.log(now.toTimeString());
    console.log(now.toLocaleString());
    console.log(now.toLocaleDateString());

    let d = new Date(1432323321);
    d.toLocaleString();
    d.toUTCString();

    // date 练习
    let today = new Date();
    if(today.getMonth() === 2 && today.getDate() === 14){
         alert('今天是情人节');
    };
    if(today.getMonth() === 2 && today.getDate() === 14){
        alert('今天是2月14日');
    }else{
        alert('今天不是2月14日');
    };

    // regexp 正则
    let re = /^\d{3}\-\d{3}\-\d{4}$/;
    let re2 = new RegExp('ABC\\-001');

    let re = /^\d{3}\-\d{3}\-\d{3,8}$/;
    re.test('123-456-789');
    re.test('123-456-7890');
    re.test('123-456-78901');

    let re  = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;   // 正则邮箱
    let i,
        success = true;
        should_pass = ['someone@gamil.com','bill.gates@microsoft.com','tom@voyager.org','bob2015@163.com'],
        should_fail = ['someone@.com.my','bob@2015','bob@.org.cn','bob@gmail.com.cn'];
    for(i=0;i<should_pass.length;i++){
        if(!re.test(should_pass[i])){
            console.log('测试失败：' + should_pass[i]);
            success = false;
            break;
        }
    }
    for (i=0;i<should_fail.length;i++){
        if(re.test(should_fail[i])){
            console.log('测试失败：' + should_fail[i]);
            success = false;
            break;
        }
    }
    if(success){
        console.log('测试成功！');
    }

    // json
    let xiaoming = {
        name: '小明',
        age: 14,
        gender:true,
        height: 1.70,
        grade:null,
        'middle-school':'\"W3C\" Middle School',
        skills:['JavaScript','Bootstrap','Node.js']
    };
    let s = JSON.stringify(xiaoming);
    console.log(s);

    function convert(key,value){
        if(typeof value === 'string'){
            return value.toUpperCase();
        }
        return value;
    }
    JSON.stringify(xiaoming,convert, ' ')

    // 反序列化
    JSON.parse('[1,2,3,true]');
    JSON.parse('{"name":"小明"，"age":14}');
    JSON.parse('true');
    JSON.parse('"Hello World"');
    JSON.parse('null');
    JSON.parse('-12.34');
    let obj = JSON.parse('{"name":"小明"，"age":14}',function(key,value){
        if(key === 'name'){
            return value.toUpperCase();
        }
        return value;
    });
    console.log(JSON.stringify(obj));


    // 测试  获取天气
    let url = 'https://api.openweathermap.org/data/2.5/forecast?q=Beijing,cn&appid=800f49846586c3ba6e7052cfc89af16c';
    fetch( url).then(
        resp=>{
            resp.json().then(data =>{
                let info = {
                    city: data.city.name,
                    weather: data.list[0].weather[0].description,
                    time: data.list[0].dt_txt
                };
                alert(JSON.stringify(info,null, ' '));
            });
        }
    );

</script>
</html>